﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;

namespace CoachingOrganizer.Android.Adapters.Tools.MoneyCalculation
{
    public class ReportAdapter : BaseAdapter
    {
        private readonly Activity _context;
        private readonly MoneyCalculationModel _model;
        private List<PeriodGroupReportModelBase> _items = new List<PeriodGroupReportModelBase>();

        public ReportAdapter(Activity context, MoneyCalculationModel model)
            : base()
        {
            _context = context;
            _model = model;
            model.Report.CurrentPeriodReportChanged += Report_CurrentPeriodReportChanged;
            UpdateItems();
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get { return _items.Count; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            int layoutId = _items[position].Sign > 0 ? Resource.Layout.MoneyCalculationIncomeReportItemLayout : Resource.Layout.MoneyCalculationSpentReportItemLayout;
            View view = _context.LayoutInflater.Inflate(layoutId, parent, false);
            if (_items[position].Sign > 0)
                HookIncomeModelToView(view, _items[position]);
            else
                HookSpentModelToView(view, _items[position]);
            return view;
        }

        private void HookIncomeModelToView(View view, PeriodGroupReportModelBase periodGroupReportModelBase)
        {
            TextView incomeNameTextView = view.FindViewById<TextView>(Resource.Id.incomeNameTextView);
            incomeNameTextView.Text = periodGroupReportModelBase.Name;
            TextView incomeAmountTextView = view.FindViewById<TextView>(Resource.Id.incomeAmountTextView);
            incomeAmountTextView.Text = periodGroupReportModelBase.GetAmount().ToString("0.00");
        }

        private void HookSpentModelToView(View view, PeriodGroupReportModelBase periodGroupReportModelBase)
        {
            TextView spentNameTextView = view.FindViewById<TextView>(Resource.Id.spentNameTextView);
            spentNameTextView.Text = periodGroupReportModelBase.Name;
            TextView spentAmountTextView = view.FindViewById<TextView>(Resource.Id.spentAmountTextView);
            spentAmountTextView.Text = periodGroupReportModelBase.GetAmount().ToString("0.00");
        }

        private void UpdateItems()
        {
            PeriodReportModel period = _model.Report.CurrentPeriodReport;
            if (period != null)
                _items = period.EnumerateIncomesGroups().Union(period.EnumerateSpentsGroups()).ToList();
        }

        private void Report_CurrentPeriodReportChanged(object sender, EventArgs e)
        {
            UpdateItems();
            NotifyDataSetChanged();
        }
    }
}